package com.jintoufs.web.action.image;

import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.images.Images;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.hotel.HotelRoomService;
import com.jintoufs.service.hotel.HotelService;
import com.jintoufs.service.image.ImageService;
import com.jintoufs.service.scene.SceneService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/images")
public class ImageAction extends SupportAction {

    @Resource
    private ImageService imageService;
    @Resource
    private CustomerService customerService;
    @Resource
    private HotelRoomService hotelRoomService;
    @Resource
    private SceneTicketService sceneTicketService;
    @Resource
    private TourRouteService tourRouteService;

    @RequestMapping("/setImages")
    @SystemControllerLog(description = "设置图册")
    public String images(String valueId, Model model, @CurrentUser Customer customer){
        Map<String,Object> param = new HashMap<>();
        param.put("valueId",valueId);
        param.put("customerId",customer.getId());
        List<Images> imagesList = imageService.selectByparams(param);
        model.addAttribute("images",imagesList);
        model.addAttribute("valueId",valueId);

        return "/images/images";
    }

    @RequestMapping("/addImages")
    @ResponseBody
    public AjaxMsg addImage(String valueId, @RequestParam("file[]") CommonsMultipartFile[] files,@CurrentUser Customer customer){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Images images;
        boolean cover = true;

        List<Map<String,Object>> maps;
        Map<String,Object> param = new HashMap<>();
        param.clear();
        param.put("cover","1");
        param.put("valueId",valueId);
        if (imageService.selectByparams(param).size()>0){
            cover = false;
        }
        param.clear();
        param.put("id",valueId);
        maps = hotelRoomService.selectByParams(param);
        if (maps.size()>0){
            param.clear();
            param.put("valueId",maps.get(0).get("hotelId"));
        }else {
            maps = sceneTicketService.queryByParams(param);
            if (maps.size()>0){
                param.clear();
                param.put("valueId",maps.get(0).get("sceneId"));
            }else {
                maps = tourRouteService.queryByParams(param);
                if (maps.size()>0){
                    param.clear();
                    param.put("valueId",maps.get(0).get("travelAgencyId"));
                }else {
                    param.clear();
                    param.put("valueId",valueId);
                }
            }
        }
        param.put("customerId",customer.getId());
        if (customerService.auth(param)){
            String attachment = imageService.upToCos(files);
            String[] pathList = attachment.split(",");
            for (String path:pathList){
                if (!path.contains("error")){
                    images = new Images();
                    images.setId(getUUID());
                    images.setPath(path);
                    images.setCreateTime(new Date());
                    images.setValueId(valueId);
                    if (cover){
                        images.setCover("1");
                    }
                    imageService.insert(images);
                }
                cover=false;
            }
        }
        return ajaxMsg;
    }
    @RequestMapping("delete")
    @ResponseBody
    public AjaxMsg delete(String id){
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            imageService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            ajaxMsg.setCode("500");
        }
        return ajaxMsg;
    }
}
